package cn.tedu.tmall.admin.mall.controller;
import cn.tedu.tmall.common.web.JsonResult;
import cn.tedu.tmall.admin.mall.pojo.pram.MallCategoryAddParam;
import cn.tedu.tmall.admin.mall.pojo.vo.MallCategoryTreeVO;
import cn.tedu.tmall.admin.mall.pojo.vo.MallCategoryVO;
import cn.tedu.tmall.admin.mall.service.IMallCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/mall/categories")
@Tag(name="类别接口")
@Validated
@Slf4j
public class CategoryController {
    @Autowired
    private IMallCategoryService mallCategoryService;

    @Operation(summary = "增加类别")
    @PostMapping("/add")
    public JsonResult add(@Validated @RequestBody MallCategoryAddParam mallCategoryAddParam) {
        log.debug("增加类别接口-入参:{}",mallCategoryAddParam);
        System.out.println("categoryAddParam = " + mallCategoryAddParam.getName());
        mallCategoryService.add(mallCategoryAddParam);
        JsonResult ok = JsonResult.ok();
        log.debug("增加类别接口-入参:{},出参:{}",mallCategoryAddParam,ok);
        return ok;
    }
    //延迟加载
    @Operation(summary = "类别列表-顶级")
    @GetMapping("/get")
    public JsonResult topCategory() {
        List<MallCategoryVO> categoryTreeVOS = mallCategoryService.topCategory();
        return JsonResult.ok(categoryTreeVOS);
    }
    @Operation(summary = "类别列表-子级")
    @GetMapping("/get/{parentId}")
    public JsonResult topCategory(@PathVariable Long parentId) {
        List<MallCategoryVO> categoryVOS = mallCategoryService.topCategory(parentId);
        return JsonResult.ok(categoryVOS);
    }


    @Operation(summary = "一次加载全部数据")
    @GetMapping("/tree")
    public JsonResult get() {
        //树结构
        List<MallCategoryTreeVO> categoryTreeVOS = mallCategoryService.getAll();
       return JsonResult.ok(categoryTreeVOS);
    }
}
